package com.woniuxy.service.impl;

import com.woniuxy.dao.DrugDao;
import com.woniuxy.dao.DrugRepertoryDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DrugServiceImpl implements DrugService {

    @Resource
    DrugDao drugDao;
    @Resource
    DrugRepertoryDao drugRepertoryDao;


    @Override
    public PageBean<Drug> selectPage(Drug drug, int pageIndex, int pageSize) {

        //        查询药品两表是否过期和是否库存为零，修改状态
        List<DrugRepertory> drugRepertoryList=drugRepertoryDao.selectdr();
        List<Drug> drugLists=drugDao.selectAll();
        for(DrugRepertory du:drugRepertoryList){
            if (du.getRepertorySum() == 0) {
                drugRepertoryDao.updateStatus(2,du.getRepertoryId());
            } else {
                System.out.println("aaa-----"+du.getRepertoryMassdate().getTime());
                System.out.println("bbb-----"+new Date().getTime());
                if (du.getRepertoryMassdate().getTime() <= new Date().getTime()) {
                    drugRepertoryDao.updateStatus(3,du.getRepertoryId());
                }
            }
        }
        for(Drug tu:drugLists){
            if (tu.getDrugSum() == 0) {
                drugDao.updateStatus(tu.getDrugId(),2);
            } else {
                if (tu.getDrugMassdate().getTime() <= new Date().getTime()) {
                    drugDao.updateStatus(tu.getDrugId(),3);
                }
            }
        }

        PageBean<Drug> drugPageBean=new PageBean<Drug>();
//        存储当前页
        drugPageBean .setPageIndex(pageIndex);
//        存储页大小
        drugPageBean .setPageSize(pageSize);
//        存储总条数
        int pageCount = drugDao.selectAllGoods(drug);
        drugPageBean.setTotalRecord(pageCount);
//        存储当前页数据

        List<Drug> drugList=drugDao.selectAllDrug(drug, pageIndex, pageSize);

        for(Drug du:drugList){
            if(du.getDrugStatus()==2){
                du.setStatus("无货，请上货");
            }else if(du.getDrugStatus()==1){
                du.setStatus("有货");
            }else{
                du.setStatus("过期，请下架");
            }

            DrugKind drugKind= drugDao.selectKing(du.getKindId());
            du.setKind(drugKind.getKindName());
        }

        drugPageBean.setBeanList(drugList);





        return drugPageBean;



    }

//    药品补货
    @Override
    public DrugSum getSum(String repertoryName) {

        DrugRepertory drugRepertory=drugRepertoryDao.selectSum(repertoryName);
        DrugSum drugSum=new DrugSum();
        if(drugRepertory.getRepertoryStatus()==2){
            drugSum.setStatus("库存为零，请先进货");
        }else if(drugRepertory.getRepertoryStatus()==3){
            drugSum.setStatus("药品已过期，请先进货");
        }else{
            drugSum.setSum(drugRepertory.getRepertorySum());
        }
        return drugSum;
    }

//    药品确认

    @Override
    public void sureDrug(String repertoryName, Integer repertorySum, Integer drugSum) {

        DrugRepertory drugRepertory=drugRepertoryDao.selectSum(repertoryName);
        if(repertorySum==0){
            drugRepertoryDao.updateStatus(2,drugRepertory.getRepertoryId());
        }else{
            drugRepertoryDao.updateSum(repertorySum,drugRepertory.getRepertoryId());
        }

        drugDao.updateDr(repertoryName,drugRepertory.getRepertoryMassdate(),drugRepertory.getRepertoryDate(),
                drugSum,drugRepertory.getRepertoryPrice());
    }

    @Override
    public void deleteDrug(String drugName) {
        drugDao.deleteDrug(drugName);
        drugRepertoryDao.deleteDr(drugName);
    }

    @Override
    public Drug selectDrugNameById(Integer drugId) {
        return drugDao.selectDrugNameById(drugId);
    }

}
